package org.fmi.lje;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.log4j.Logger;
import org.fmi.lje.main.ProjectExecutor;
import org.fmi.lje.model.UmlGenProject;
import org.fmi.lje.model.util.ProjectUtil;
import org.fmi.lje.modules.AntProjectCompilerModule;
import org.fmi.lje.modules.AntProjectLoaderModule;
import org.fmi.lje.modules.DummyDiagramModule;
import org.fmi.lje.uml.Diagram;
import org.fmi.lje.uml.svg.SvgDiagram;
import org.w3c.dom.Element;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {

	private static final Logger log = Logger.getLogger(Main.class);

	public static void main(String args[]) throws IOException {
		Injector injector = Guice.createInjector(new AntProjectCompilerModule(), new AntProjectLoaderModule(), new DummyDiagramModule());

		ProjectExecutor projectExecutor = injector.getInstance(ProjectExecutor.class);

		UmlGenProject project = ProjectUtil.loadProjectFromFolder(new File(args[0]));

		List<Diagram> diagrams = projectExecutor.execute(project);

		for (Diagram diagramGeneral : diagrams) {
			SvgDiagram diagram = (SvgDiagram) diagramGeneral;
			diagram.draw();

			// Populate the document root with the generated SVG content.
			Element root = diagram.getDocumentElement();
			diagram.getRoot(root);

			// Display the document.
			JSVGCanvas canvas = new JSVGCanvas();
			JFrame f = new JFrame();
			f.getContentPane().add(canvas);
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			canvas.setSVGDocument(diagram.getDocument());

			// System.out.println(xmlToString(diagram.getDocument()));
			f.pack();
			f.setVisible(true);
		}
	}
}
